<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    :bodyStyle="bodyStyle"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top:15%;height: 70%;overflow-y: hidden"
  >
    <template slot="footer">
      <a-button key="back" v-if="isReadOnly" @click="handleCancel">
        关闭
      </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" id="customerModal">
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="客户名称">
            <a-input placeholder="请输入客户名称" v-decorator.trim="['name', validatorRules.name]" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="所属行业">
            <a-input placeholder="请输入所属行业" v-decorator.trim="['industry', validatorRules.required]" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="省份">
            <a-select
              v-decorator.trim="['province']"
              allowClear
              placeholder="选择"
              showSearch
              optionFilterProp="children"
            >
              <a-select-option v-for="(item, index) in province" :key="index" :value="item">
                {{ item }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="主要对接人">
            <a-input placeholder="请输入主要对接人" v-decorator.trim="['contact', validatorRules.required]" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="职位">
            <a-input placeholder="请输入职位" v-decorator.trim="['work', validatorRules.required]" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系方式">
            <a-input placeholder="请输入联系方式" v-decorator.trim="['phone', validatorRules.required]" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="地址">
            <a-input placeholder="请输入地址" v-decorator.trim="['addr']" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="客户规模">
            <a-input placeholder="请输入客户规模" v-decorator.trim="['customerSize']" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="财政状况">
            <a-input placeholder="请输入财政状况" v-decorator.trim="['financialSituation']" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="销售代表">
            <a-input placeholder="请输入销售代表" v-decorator.trim="['salesDirector']" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item label="录入时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-date-picker
              style="width: 100%"
              v-decorator.trim="['entryTime']"
              format="YYYY-MM-DD"
              placeholder="请输入录入时间"
            />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注">
            <a-textarea :rows="2" placeholder="请输入备注" v-decorator.trim="['remark']" />
          </a-form-item>
        </a-col>
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
import pick from 'lodash.pick'
import { addCustomer, editSupplier, checkSupplier } from '@/api/api'
import { autoJumpNextInput } from '@/utils/util'
export default {
  name: 'CustomerModal',
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      isReadOnly: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      bodyStyle: {
        padding: '20',
        height: window.innerHeight * 0.6 + 'px',
        'overflow-y': 'auto'
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      //省份--前端写死
      province: [
        '北京市',
        '天津市',
        '上海市',
        '重庆市',
        '河北省',
        '山西省',
        '辽宁省',
        '吉林省',
        '黑龙江省',
        '江苏省',
        '浙江省',
        '安徽省',
        '福建省',
        '江西省',
        '山东省',
        '河南省',
        '湖北省',
        '湖南省',
        '广东省',
        '海南省',
        '四川省',
        '贵州省',
        '云南省',
        '陕西省',
        '甘肃省',
        '青海省',
        '台湾省',
        '香港特别行政区',
        '澳门特别行政区',
        '内蒙古自治区',
        '广西壮族自治区',
        '西藏自治区',
        '宁夏回族自治区',
        '新疆维吾尔自治区'
      ],
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        name: {
          rules: [
            { required: true, message: '请输入名称!' },
            { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' },
            { validator: this.validateSupplierName }
          ]
        },
        required: {
          rules: [{ required: true, message: '请输入!' }]
        }
      }
    }
  },
  created() {},
  methods: {
    add() {
      this.edit()
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      console.log(this.model)
      this.$nextTick(() => {
        //如果有record则走编辑,日期回显
        if (record) {
          var entryTime = this.$moment(this.model.entryTime)
          this.form.setFieldsValue({ entryTime: entryTime })
        }
        this.form.setFieldsValue(
          pick(
            this.model,
            'province',
            'name',
            'contact',
            'phone',
            'work',
            'industry',
            'addr',
            'customerSize',
            'financialSituation',
            'salesDirector',
            'remark'
          )
        )
        autoJumpNextInput('customerModal')
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.isReadOnly = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          console.log(this.model)
          let formData = Object.assign(this.model, values)
          if (this.model.beginNeedGet && this.model.beginNeedPay) {
            that.$message.warn('期初应收和期初应付不能同时输入')
            that.confirmLoading = false
            return
          }
          // formData.type = '客户'
          formData.entryTime = this.$moment(formData.entryTime).format('YYYY-MM-DD')
          let obj
          if (!this.model.id) {
            obj = addCustomer(formData)
          } else {
            obj = addCustomer(formData)
          }
          obj
            .then(res => {
              if (res.code === 200 || res.code === 10000) {
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    validateSupplierName(rule, value, callback) {
      let params = {
        name: value,
        type: '客户',
        id: this.model.id ? this.model.id : 0
      }
      checkSupplier(params).then(res => {
        if (res && res.code === 200) {
          if (!res.data.status) {
            callback()
          } else {
            callback('名称已经存在')
          }
        } else {
          callback(res.data)
        }
      })
    }
  }
}
</script>
<style scoped></style>
